package servlets;

import dbhandler.DBConnector;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
import java.net.*;
import java.security.SecureRandom;
import java.util.regex.*;
import org.apache.commons.lang.WordUtils;

public class Validate {

    private String firstName;
    private String lastName;
    private String middleInitial;
    private String email;
    private String userName;
    private String password1;
    private String password2;
    private Hashtable errors;

    public boolean validate() throws SQLException, ClassNotFoundException {
        boolean bool = true;
        DBConnector dbConnectorchk = null;
        dbConnectorchk = new DBConnector("localhost", 3306, "selector", "53l3ct0r", "bookaholic");
//^[a-zA-Z]+(([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$
        firstName = WordUtils.capitalizeFully(firstName);
        if (firstName.equals("") || !(firstName.matches("^[a-zA-Z]+(([\\'\\,\\.\\-\\s][a-zA-Z])?[a-zA-Z]*)*$"))) {
            errors.put("firstName", "Please enter your first name (up to two words)");
            firstName = "";
            bool = false;

        }
        lastName = WordUtils.capitalizeFully(lastName);
        if (lastName.equals("") || !(firstName.matches("^[a-zA-Z]+(([\\'\\,\\.\\-\\s][a-zA-Z])?[a-zA-Z]*)*$"))) {
            errors.put("lastName", "Please enter your last name (up to two words)");
            lastName = "";
            bool = false;
        }

        if (middleInitial.equals("") || middleInitial.length() != 1 || !(middleInitial.matches("[A-Z]"))) {
            errors.put("middleInitial", "Please enter 1 uppercase Middle Initial");
            middleInitial = "";
            bool = false;
        }


        if (email.equals("") || !(email.matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"))) {
            errors.put("email", "Please enter a valid email address");
            email = "";
            bool = false;
        } else if (!dbConnectorchk.checkemail(email)) {
            errors.put("email", "Email already exists");
            email = "";
            bool = false;
        }

        if (userName.equals("") || userName.length() < 6 || !(userName.matches("(?=^.{6,}$)(?=.*[a-zA-Z])(?=.*[\\d])(?!.*[\\W_\\x7B-\\xFF]).*$"))) {
            errors.put("userName", "Please enter valid a username (minimum length of 6 characters with at least 1 numerical)");
            userName = "";
            bool = false;
        } else if (!dbConnectorchk.checkusername(userName)) {
            errors.put("userName", "username already exists");
            userName = "";
            bool = false;
        }

        if (password1.equals("") || password1.length() < 7 || !(password1.matches("(?=^.{8,}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[\\W_\\d]).*$"))) {
            errors.put("password1", "Please enter a valid password (minimum length of 8 characters long containing upper, lower, and 1 numerical/special characters)");
            password1 = "";
            bool = false;
        }
        if (!password1.equals("") && (password2.equals("")
                || !password1.equals(password2))) {
            errors.put("password2", "Please confirm your password");
            password2 = "";
            bool = false;
        }

        if (bool == true) {
            try {

                DBConnector dbConnector = null;
                dbConnector = new DBConnector("localhost", 3306, "adder", "15mAdd3r", "bookaholic");

                SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
                byte[] bSalt = new byte[32];
                sr.nextBytes(bSalt);
                byte[] bDigest = dbConnector.getHash(password1, bSalt);
                String sDigest = dbConnector.byteToBase64(bDigest);
                String sSalt = dbConnector.byteToBase64(bSalt);

                //Create new user
                dbConnector.createUser(userName, password1, sDigest, sSalt, firstName, lastName, middleInitial, email, 1);
                firstName = "";
                lastName = "";
                email = "";
                userName = "";
                password1 = "";
                password2 = "";
                middleInitial = "";
                errors = new Hashtable();
            } catch (Exception e) {
                System.out.println("Exception is ;" + e);
            }

        }
        return bool;
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

    public Validate() {
        firstName = "";
        lastName = "";
        email = "";
        userName = "";
        password1 = "";
        password2 = "";
        middleInitial = "";
        errors = new Hashtable();
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword1() {
        return password1;
    }

    public String getPassword2() {
        return password2;
    }

    public String getmiddleInitial() {
        return middleInitial;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public void setmiddleInitial(String middleInitial) {
        this.middleInitial = middleInitial;
    }

    public void setErrors(String key, String msg) {
        errors.put(key, msg);
    }
}
